﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Dataglot.Utils;

namespace Dataglot.OleDb
{
    class OleDbMaker
    {
        /// <summary>
        /// Assume some oleDb connection string exists in config file
        /// </summary>
        /// <param name="oleDbConnectionStringName"></param>
        public DataFactory Make(string oleDbConnectionStringName)
        {
            TraceApp.Verbose(() => "Oledb constructor, will extract provider from conn string tag");
            if(string.IsNullOrEmpty(oleDbConnectionStringName))
            {
                throw new ArgumentException("Missing param, need an oleDbConnectionString name", oleDbConnectionStringName);
            }
            //ValidateConnectionString(oleDbConnectionStringName);
            //ConnectionStringSettings c = ConfigurationManager.ConnectionStrings[oleDbConnectionStringName];
            ////Infer provider?
            //oleDbConnectionString = c.ConnectionString;
            
            ////This is as native as we can get.
            //nativeConnectionString = c.ConnectionString;

            //string providerName;
            //if (oleDbConnectionStringName=="LocalMySqlServer" && string.IsNullOrEmpty(c.ProviderName))
            //{
            //    //Added by mysql?
            //    providerName = "MySql.Data.MySqlClient";
            //    brand = DbBrand.MySql;
            //}
            //else
            //{
            //    providerName = c.ProviderName;
            //}

            //if (String.IsNullOrEmpty(providerName))
            //{
            //    throw new InvalidOperationException("Expected a provider name in <connectionStrings/> in app.config or web.config for con string " + oleDbConnectionStringName);
            //}
            //provider = DbProviderFactories.GetFactory(providerName);
            //ConvertProvidertoBrand(providerName);
            return new DataFactory();
        }
        public static string ConvertNativeSqlToOleDb(string native)
        {
            //Native
            //"Data Source=.\sqlexpress;Initial Catalog=mydb;Integrated Security=SSPI" 
            //data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnetdb.mdf;User Instance=true

            string[] nativeParts = native.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
            Dictionary<string, string> table = new Dictionary<string, string>(4);
            foreach (string s in nativeParts)
            {
                string[] nameValuePair = s.Split(new char[] { '=' }, StringSplitOptions.RemoveEmptyEntries);
                table.Add(nameValuePair[0].ToLower(), nameValuePair[1]);
            }

            //Oledb
            //"Provider=SQLNCLI;Server=.\sqlexpress;Database=mydb;Integrated Security=SSPI;"
            //Provider=SQLNCLI;Server=.\SQLExpress;AttachDbFilename=c:\mydbfile.mdf; Database=dbname;Trusted_Connection=Yes;
            //Provider=SQLNCLI;Server=.\SQLExpress;AttachDbFilename=|DataDirectory|mydbfile.mdf; Database=dbname;Trusted_Connection=Yes;

            //data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnetdb.mdf;User Instance=true
            string oledb;
            if (native.ToLower().Contains("user instance"))
            {
                oledb = String.Format("data source={0};Integrated Security=SSPI;AttachDBFilename={1};User Instance=true",
                                         table["data source"], table["attachdbfilename"]);
            }
            else
                oledb = String.Format("Provider=SQLNCLI;Server={0};Database={1};Integrated Security=SSPI;",
                                             table["data source"], table["initial catalog"]);
            return oledb;

        }

    }
}
